--- 战策六·招兵买马
local policy6 = fk.CreateSkill {
    name = "buy_arm",
    tags = { Skill.Compulsory },
    dynamic_name = function (self, player, lang)
      return player == Self and self.name or "unknownpolicy"
    end,
    dynamic_desc = function (self, player, lang)
      return player == Self and self.name or "unknownpolicy"
    end,
}

local HG = require "packages.new_heg_diy_my.util"

Fk:loadTranslationTable{
    ["@[buy_arm_mark]"] = "招兵买马",
    ["buy_arm_ask"] = "战策-招兵买马：你可以弃置一张牌，检索并获得一张装备牌",
}

policy6:addEffect(fk.EventPhaseStart, {
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
        local mark_name = "@[private]" .. policy6.name .. "_mark"
        return player:getMark(mark_name) ~= 0 and player.phase == Player.Start and target == player
    end,
    on_cost = function(self, event, target, player, data)
        local card = player.room:askToDiscard(player, {
            min_num = 1,
            max_num = 1,
            include_equip = false,
            skill_name = "unknownpolicy",
            prompt = "buy_arm_ask",
            cancelable = true,
            skip = true,
        })
        if #card > 0 then
            event:setCostData(self, { card = card })
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local mark_name = "@[private]" .. policy6.name .. "_mark"
        local pattern = "equip"
        local card
        local cards = {}
        room:throwCard(event:getCostData(self).card, policy6.name, player, player)
        local _pattern = ".|.|.|.|.|equip"
        if #room:getCardsFromPileByRule(_pattern, 1) == 0 then return false end
        while true do
            local id = room:getNCards(1)[1]
            room:moveCardTo(id, Card.Processing, nil, fk.ReasonJustMove, policy6.name)
            room:delay(300)
            local c = Fk:getCardById(id)
            if c:getColorString() == pattern or c:getTypeString() == pattern then
                card = c
                break
            else
                table.insert(cards, id)
            end
        end
        room:moveCardTo(card.id, Player.Hand, player, fk.ReasonPrey, policy6.name, nil, true, player)
        cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
        room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, policy6.name, nil, true, player)
        room:setPlayerMark(player, mark_name, 0)
        room:handleAddLoseSkills(player, "-buy_arm", nil, false)
        room.logic:trigger(HG.HegLosePolicy, player, { player, policy6.name })
        local card1 = Fk:cloneCard("heg_buy_arm")
        room:getVirtCardId(card1)
        room:showVirtualCard(card1, player, nil, room.logic:getCurrentEvent().id + 1)
    end,
})

return policy6
